package com.zhangzhan.blog_service.service.impl;


import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhangzhan.base.constant.StatusCode;
import com.zhangzhan.base.common_model.result.ResponseResult;
import com.zhangzhan.base.utils.CopyBean;
import com.zhangzhan.blog_model.entity.Article;
import com.zhangzhan.blog_model.entity.Category;
import com.zhangzhan.blog_model.vos.ArticleDetailVo;
import com.zhangzhan.blog_model.vos.HotArticleVo;
import com.zhangzhan.blog_model.vos.PageArticleVo;
import com.zhangzhan.blog_model.vos.PageVo;
import com.zhangzhan.blog_service.mapper.ArticleMapper;
import com.zhangzhan.blog_service.service.ArticleService;
import com.zhangzhan.blog_service.service.CategoryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 文章表 服务实现类
 * </p>
 *
 * @author zhangzhan
 * @since 2024-01-30
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private CategoryService categoryService;

    /**
     * @return com.zhangzhan.blog_base.model.ResponseResult<java.util.List < com.zhangzhan.admin_model.vo.HotArticleVo>>
     * @description 查询热文章
     * @author zhangyiheng
     * @date 2024/1/30 21:46
     */
    @Override
    public ResponseResult<List<HotArticleVo>> hotArticleList() {

        //需要查询浏览量最高的前10篇文章的信息。要求展示文章标题和浏览量。把能让用户自己点击跳转到具体的文章详情进行浏览。
        //不能把草稿展示出来，不能把删除了的文章查询出来。要按照浏览量进行降序排序。

        IPage<Article> page = new Page<>(1, 10);
        page(page, Wrappers.<Article>lambdaQuery()
                .eq(Article::getDelFlag, StatusCode.ND)
                .orderByDesc(Article::getViewCount)).getRecords();
        return ResponseResult.okResult(CopyBean.copyBeanList(page.getRecords(), HotArticleVo.class));
    }

    /**
     * @param pageNum
     * @param pageSize
     * @param categoryId
     * @return com.zhangzhan.blog_base.model.ResponseResult<java.util.List < com.zhangzhan.admin_model.vos.PageVo>>
     * @description 分页查询文章列表
     * @author zhangyiheng
     * @date 2024/1/31 22:37
     */
    @Override
    public ResponseResult<List<PageVo>> articleList(Long pageNum, Long pageSize, Long categoryId) {
        //在首页和分类页面都需要查询文章列表。
        // 首页：查询所有的文章分类页面：查询对应分类下的文章要求 需要分頁：
        // ①只能查询正式发布的文章②置顶的文章要显示在最前面
        //根据categoryId判断查询首页还是分类页面
        IPage<Article> page = new Page<>(pageNum, pageSize);
        page(page, Wrappers.<Article>lambdaQuery()
                .eq(Article::getStatus, StatusCode.S_ND)
                .eq(Objects.isNull(categoryId) || categoryId > 0, Article::getCategoryId, categoryId)
                .orderByDesc(Article::getIsTop));

        List<PageArticleVo> articleVos = CopyBean.copyBeanList(page.getRecords(), PageArticleVo.class);

        //查询分类名称
        if (Objects.isNull(categoryId) || categoryId >= 0) {
            List<Long> categoryIds = page.getRecords().stream().map(Article::getCategoryId).collect(Collectors.toList());
            List<Category> categories = categoryService.listByIds(categoryIds);
            for (Category category : categories) {
                page.getRecords().forEach(po -> {
                    if (NumberUtil.equals(po.getCategoryId(), category.getId())) {
                        articleVos.forEach(vo -> {
                            if (NumberUtil.equals(vo.getId(), po.getId())) vo.setCategoryName(category.getName());
                        });
                    }
                });
            }
        } else {
            articleVos.forEach(po -> po.setCategoryName(categoryService.getById(categoryId).getName()));
//            articleVos.removeIf(po -> StrUtil.isBlank(po.getCategoryName()));
        }

        PageVo pageVo = new PageVo();
        pageVo.setRows(articleVos);
        pageVo.setTotal(page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    /**
     * @param id
     * @return com.zhangzhan.blog_base.model.ResponseResult<com.zhangzhan.admin_model.vos.ArticleDetailVo>
     * @description 文章详情接口
     * @author zhangyiheng
     * @date 2024/2/1 22:35
     */
    @Override
    public ResponseResult<ArticleDetailVo> articleDetail(Long id) {
//        要求在文章列表点击阅读全文时能够跳转到文章详情页面，可以让用户阅读文章正文。
//        要求：①要在文章详情中展示其分类名

        Article article = this.getById(id);
        if (Objects.isNull(article)) {
            throw new RuntimeException("文章内容展示出错！");
        }
        ArticleDetailVo articleDetailVo = CopyBean.copyBean(article, ArticleDetailVo.class);
        Category category = categoryService.getById(article.getCategoryId());

        String categoryName = Objects.isNull(category) ? "暂无文章内容" : category.getName();

        articleDetailVo.setCategoryName(categoryName);
        return ResponseResult.okResult(articleDetailVo);
    }
}
